<?php



class DiscussAction extends ApiAction {
    public function top() {

        $aId = I('get.aid');
        $map = array();
        $DiscussDb = D('Discuss');

        $map['aId'] = $aId;

        $list = $DiscussDb->where($map)->order('date')->select();
        $ret['result'] = 'ok';
        $ret['discuss'] = $list;
        echo json_encode($ret);
    }

    public  function Add(){

        try{
            $data['aId']=I('get.aId');
            $data['pid']=I('get.dId');
            $data['content']=I('get.content');
            $data['date']=date("Y-m-d H:i:s");
            $data['wxUid']=I('get.wxuId');
            $data['bwxName']=I('get.bwxName');
            $DiscussDb = D('Discuss');

            $did = $DiscussDb->data($data)->add();


            $a = D('Answer')->where(array('aId'=>I('get.aId')))->find();
            $u = D('Wxuser')->join('join tp_member on tp_wxuser.member_id=tp_member.member_id')->where(array('wxUid'=>I('get.wxuId')))->find();

            $ms='';
            $formid='';
            $openid=''; 
            $userid='';
            $qid='';

            if(I('get.dId')>0){
                $d = D('Discuss')->where(array('dId'=>I('get.dId')))->find();
                $ms=$u['nickname'].' 评论了你的评论（'.$d['content'].'）'; 
                $m = D('Message')->where(array('did'=>I('get.dId')))->find();
                $userid=$m['wxuid'];
                $formid=$m['formId'];
                $qid=$m['qid'];
            }else
            {
                $ms=$u['nickname'].' 评论了你的回答（'.$a['content'].'）';
                $m = D('Message')->where(array('aid'=>I('get.aId')))->find();
                $userid=$m['wxuid'];
                $formid=$m['formId'];
                $qid=$m['qid'];
            }
            D('Message')->data(array('message'=>$ms,'qid'=>$a['qId'],'did'=>$did,'readed'=>0,'date'=>date("Y-m-d H:i:s"),'wxuid'=>I('get.wxuId'),'doWxuid'=> $userid,'formId'=>I('get.formId')))->add();

            //send_weapp_msg($touser,$template_id,$page,$form_id,$data,$emphasis_keyword=NULL)
            //申请信息模板
            $qu = D('Wxuser')->join('join tp_member on tp_wxuser.member_id=tp_member.member_id')->where(array('wxUid'=>$userid))->find();

            $val=array( 
                "keyword1"=>array('value'=>$u['nickname']), 
                "keyword2"=>array('value'=>I('get.content')), 
                "keyword3"=>array('value'=>date("Y-m-d H:i:s")),
                "keyword4"=>array('value'=>$ms),
            ); 
            $ret['data']=$val;
            $ret['opid']=$qu['open_id'];
            $ret['tempRes'] = send_weapp_msg($qu['open_id'],'hYsf75JtVJYKd5N42EXznpzTAxXaYTGehagPYIuLEX4','wenzhang/wenzhang?id='.$qid,$formid,$val,'');

            $ret['result'] = 'ok';
            $ret['time']=date("Y-m-d H:i:s");
        }catch(\Exception $e){
            $ret['ms'] = $e->getMessage();
        }
        echo json_encode($ret);
    }


    public function upLike(){

        try {
            $DlikelogDb = D('Dlikelog');
            $map=array('dId' => I('did'),'wxUid' => I('get.wxuId'));
            $likeLog = $DlikelogDb->where($map)->find();
            $addQty=1;
            if ($likeLog != null) {
                if($likeLog['dlike']==1)
                    $addQty=-1;
                $likeLog['dlike'] = 1 - $likeLog['dlike'];
                $DlikelogDb->where($map)->data($likeLog)->save();
                $ret['type'] = '1';
            } else {
                $data = array();
                $data['dlike'] = 1; 
                $data['dId'] = I('did');
                $data['wxUid'] = I('get.wxuId');
                $DlikelogDb->data($data)->add();
                $ret['type'] = '0';
            }
 
            $WxUserDb = D('Wxuser');
            $user = $WxUserDb->where(array('wxUid'=>I('get.wxuId')))->find();
            $user['likeQty']+=$addQty;
            $WxUserDb->where(array('wxUid'=>I('get.wxuId')))->data($user)->save();

            $ret['result'] = 'ok';
            echo json_encode($ret);
        }catch(\Exception $e){
            $ret['ms'] = $e->getMessage();
            echo json_encode($ret);
        }
    }
}